<?php
/**
 *
 * @filename 项目方管理
 */
namespace app\admin\controller;

use think\Controller;
use app\admin\model\Team as Teams;
use think\Db;

class Team extends Common
{
    private $cModel;   //当前控制器关联模型

    public function _initialize()
    {
        parent::_initialize();
        $this->cModel = new Teams;   //别名：避免与控制名冲突
    }

    public function index()
    {
        $where = [];
        if (input('get.search')){
            $where['team_name|team_message|team_url|team_document'] = ['like', '%'.input('get.search').'%'];
        }
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = $order[0].' '.$order[1];
        }else{
            $order = 'id desc';
        }
        $dataList = $this->cModel->where($where)->order($order)->paginate('', false, page_param());
        $this->assign('dataList', $dataList);
        return $this->fetch();
    }
    public function create(){
        if (request()->isPost()){
            Db::startTrans();
            try{
                $data = input('post.');
                $result = $this->cModel->validate(CONTROLLER_NAME.'.add')->allowField(true)->save($data);
                $uid = $this->cModel->getLastInsID();
                // 提交事务
                if ($result && $uid){
                    Db::commit();
                    return ajaxReturn(lang('action_success'), url('index'));
                }else{
                    return ajaxReturn($this->cModel->getError());
                }
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return ajaxReturn($e->getMessage());
            }
        }else{
            return $this->fetch('create');
        }
    }

    public function edit($id){
        if (request()->isPost()){
            $data = input('post.');
            //修改信息
            if (count($data) == 2){
                foreach ($data as $k =>$v){
                    $fv = $k!='id' ? $k : '';
                }
                $result = $this->cModel->validate(CONTROLLER_NAME.'.'.$fv)->allowField(true)->save($data, $data['id']);
            }else{
                $result = $this->cModel->validate(CONTROLLER_NAME.'.edit')->allowField(true)->save($data, $data['id']);
            }

            if ($result){
                return ajaxReturn(lang('action_success'), url('index'));
            }else{
                return ajaxReturn($this->cModel->getError());
            }
        }else{
            if ($id > 0){
                $data = $this->cModel->get($id);

                $this->assign('data', $data);
                return $this->fetch();
            }
        }

    }

    public function delete()
    {
        if (request()->isPost()){
            $id = input('id');
            if (isset($id) && !empty($id)){
                Db::startTrans();
                try{
                    $id_arr = explode(',', $id);   //用户数据
                    $rechModel = new \app\admin\model\Recharge;
                    $cashModel = new \app\admin\model\Getcash;
                    $where1 = [ 'user_id' => ['in', $id_arr] ];
                    $where2 = [ 'id' => ['in', $id_arr] ];
                    $result = $this->cModel->where($where2)->delete();   //删除主表数据
                    $result2 = $rechModel->where($where1)->delete();   //删除充值记录
                    $result3 = $rechModel->where($where1)->delete();   //删除提现记录
                    // 提交事务
                    if ($result){
                        Db::commit();
                        return ajaxReturn(lang('action_success'), url('index'));
                    }else{
                        return ajaxReturn($this->cModel->getError());
                    }
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    return ajaxReturn($e->getMessage());
                }
            }
        }
    }	function checklist(){        $where = ['isCheck'=>0];        if (input('get.search')){            $where['team_name|team_message|team_url|team_document'] = ['like', '%'.input('get.search').'%'];        }        if (input('get._sort')){            $order = explode(',', input('get._sort'));            $order = $order[0].' '.$order[1];        }else{            $order = 'id desc';        }        $dataList = $this->cModel->where($where)->order($order)->paginate('', false, page_param());        $this->assign('dataList', $dataList);        return $this->fetch();	}			function isCheck($id){		if (request()->isPost()){				$data = input('post.');					$result = $this->cModel->validate(CONTROLLER_NAME.'.beizhu')->allowField(true)->save($data, $data['id']);				if ($result){					return ajaxReturn(lang('action_success'), url('Team/checklist'));				}else{					return ajaxReturn($this->cModel->getError());				}		}		else		{			$data = $this->cModel->get($id);			$this->assign('data', $data);			return $this->fetch();		}	}
}